<?php require_once('Connections/sqlaerosnemanje.php'); ?>
<?php 
header("Content-type: text/html");

// Prevzami podatek o projektu in izbranem redu
$selected_projekt = $_POST['selectProjekt'];
$selected_st_reda = $_POST['selectRed'];

// Query za izbiranje prve tocke v redu
mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_izberiTockoA = "SELECT * FROM tocke_geodetic WHERE stReda = '$selected_st_reda' AND IDProjekta = '$selected_projekt' ORDER BY imeTocke ASC LIMIT 0,1";
$izberiTockoA = mysql_query($query_izberiTockoA, $sqlaerosnemanje) or die(mysql_error());
$row_izberiTockoA = mysql_fetch_assoc($izberiTockoA);
$totalRows_izberiTocke = mysql_num_rows($izberiTockoA);
// Query za izbiranje zadnje tocke v redu
mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_izberiTockoB = "SELECT * FROM tocke_geodetic WHERE stReda = '$selected_st_reda' AND IDProjekta = '$selected_projekt' ORDER BY imeTocke DESC LIMIT 0,1";
$izberiTockoB = mysql_query($query_izberiTockoB, $sqlaerosnemanje) or die(mysql_error());
$row_izberiTockoB = mysql_fetch_assoc($izberiTockoB);
$totalRows_izberiTocke = mysql_num_rows($izberiTockoB);
// Query za ime projekta
mysql_select_db($database_sqlaerosnemanje, $sqlaerosnemanje);
$query_imeProjekta = "SELECT * FROM projekti WHERE IDP = '$selected_projekt'";
$imeProjekta = mysql_query($query_imeProjekta, $sqlaerosnemanje) or die(mysql_error());
$row_imeProjekta = mysql_fetch_assoc($imeProjekta);
$totalRows_izberiTocke = mysql_num_rows($imeProjekta);

// Zacetek izvoza v Google KML
$outFile = "KMLIzvoz/izvoz_". $row_imeProjekta['Naziv'] ."-". $selected_st_reda .".kml";
$fh = fopen($outFile, 'w') or die("Ne morem brati/pisati.");

$kml_output = "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n";
$kml_output .= "<Folder>\n";
// Uvoz linije od tocke A do B
$kml_output .= "<Placemark>\n";
$kml_output .= "<name>". $row_imeProjekta['Naziv'] ." - red ". $selected_st_reda ."</name>\n";
$kml_output .= "<Style>\n";
$kml_output .= "<LineStyle>\n";
$kml_output .= "<color>FFFF0000</color>\n";
$kml_output .= "<width>2</width>\n";
$kml_output .= "</LineStyle>\n";
$kml_output .= "</Style>\n";
$kml_output .= "<LineString id=\"red". $selected_st_reda ."\">\n";
$kml_output .= "<extrude>0</extrude>\n";
$kml_output .= "<tessellate>0</tessellate>\n";
$kml_output .= "<altitudeMode>clampToGround</altitudeMode>\n";
$kml_output .= "<coordinates>\n";
$kml_output .= round($row_izberiTockoA['lon'], 6).",".round($row_izberiTockoA['lat'], 6).",0\n";
$kml_output .= round($row_izberiTockoB['lon'], 6).",".round($row_izberiTockoB['lat'], 6).",0\n";
$kml_output .= "</coordinates>\n";
$kml_output .= "</LineString>\n";
$kml_output .= "</Placemark>\n";
// Uvoz tocke A
$kml_output .= "<Placemark>\n";
$kml_output .= "<description>".$row_izberiTockoA['imeTocke']."<br/>lat = ".round($row_izberiTockoA['lat'], 6)."<br/>lon = ".round($row_izberiTockoA['lon'], 6)."<br/>elipVisina = ".round($row_izberiTockoA['elipVisina'], 6)."</description>\n";
$kml_output .= "<name>".$row_izberiTockoA['imeTocke']."</name>\n";
$kml_output .= "<Point>\n";
$kml_output .= "<altitudeMode>clampToGround</altitudeMode>\n";
$kml_output .= "<coordinates>".round($row_izberiTockoA['lon'], 6).",".round($row_izberiTockoA['lat'], 6)."</coordinates>\n";
$kml_output .= "</Point>\n";
$kml_output .= "</Placemark>\n";
// Uvoz tocke B
$kml_output .= "<Placemark>\n";
$kml_output .= "<description>".$row_izberiTockoB['imeTocke']."<br/>lat = ".round($row_izberiTockoB['lat'], 6)."<br/>lon = ".round($row_izberiTockoB['lon'], 6)."<br/>elipVisina = ".round($row_izberiTockoB['elipVisina'], 6)."</description>\n";
$kml_output .= "<name>".$row_izberiTockoB['imeTocke']."</name>\n";
$kml_output .= "<Point>\n";
$kml_output .= "<altitudeMode>clampToGround</altitudeMode>\n";
$kml_output .= "<coordinates>".round($row_izberiTockoB['lon'], 6).",".round($row_izberiTockoB['lat'], 6)."</coordinates>\n";
$kml_output .= "</Point>\n";
$kml_output .= "</Placemark>\n";
$kml_output .= "</Folder>\n";
$kml_output .= "</kml>";

fwrite($fh, $kml_output);
fclose($fh);
echo "Ustvarjena je bila datoteka <b>".$outFile."</b> ,ki jo lahko snamete <a href=\"".$outFile."\">TUKAJ</a>.";
// echo $kml_output;
mysql_free_result($izberiTockoA);
mysql_free_result($izberiTockoB);
mysql_free_result($imeProjekta);
?>
<br><br><a href="prepare_kml.php">Nazaj na izbiro projekta</a>